<?php

class Payment extends My_Controller
{
    public function index()
    {
    	if($this->cart->total_items()>0)
    	{
            $accountid = $this->session->userdata('accountid');
            $this->load->model('m_item');
            $this->load->model('m_account');
            $data['hero'] = $this->m_item->get_all_hero();
            $data['bankaccounts'] = $this->m_account->loadbanks($accountid);
        	$this->load->view('Payment/Payment', $data);
    	}
    	else
    	{
        	$this->load->view('Cart/Shop_Cart');
    	}
    }

    public function load_list_invoice_by_username($username)
    {
    	$this->load->model('m_account');
    	$row = $this->m_account->get_account_by_username($username);
    	$id = $row->ACCOUNT_ID;

    	$this->load->model('m_payment');
    	$data['invoice_list'] = $this->m_payment->load_list_invoice_by_accountid($id);
    	$this->load->view('Payment/Payment_History', $data);
    }

    public function load_invoice_detail_by_invoiceid($id)
    {
    	$this->load->model('m_payment');
    	$data['invoice_details'] = $this->m_payment->load_invoice_detail_by_invoiceid($id);
    	$this->load->view('Cart/Cart_History', $data);
    }

    public function complete_payment($type)
    {
    	$this->load->model('m_payment');
    	date_default_timezone_set('Asia/Ho_Chi_Minh');
    	$now = getdate();
    	$start_date = $now['year']."-".$now['mon']."-".$now['mday']." ".$now['hours'].":".$now['minutes'].":".$now['seconds'];
    	$accountid = $this->session->userdata('accountid');
    	$total = $this->cart->total();
    	
    	
    	if($type == 'WALLET')
    	{
    		if($this->session->userdata('wallet') >= $total)
    		{
    			if($this->m_payment->insert_invoice($accountid, $type, $start_date, $total, 1, ''))
		    	{
                    $newwallet = $this->session->userdata('wallet') - $total;

		    		$invoice_id = $this->db->insert_id();

			    	$data['type'] = $type;
                    $data['invoice_id'] = $invoice_id;
                    $data['total'] = $total;

			    	foreach($this->cart->contents() as $cart_items)
			    	{
			    		$this->m_payment->insert_invoice_detail($invoice_id, $cart_items['id'], $cart_items['qty']);
			    	}

                    $this->load->model('m_account');
                    $cc = $this->m_account->update_wallet($accountid, $start_date, $newwallet);
                    $this->session->set_userdata('wallet', $newwallet );

			    	$this->cart->destroy();

                    $data['invoice_details'] = $this->m_payment->load_invoice_detail_by_invoiceid($invoice_id);

			    	$data['isdone'] = true;
			        $this->load->view('Payment/Complete_payment', $data);
		    	}
    		}
    	}

    	else if($type == 'BK')
    	{
    		if($this->m_payment->insert_invoice($accountid, $type, $start_date, $total, 1, ''))
	    	{
	    		$invoice_id = $this->db->insert_id();

		    	$data['type'] = $type;
                $data['invoice_id'] = $invoice_id;
                $data['total'] = $total;

		    	foreach($this->cart->contents() as $cart_items)
		    	{	
		    		$this->m_payment->insert_invoice_detail($invoice_id, $cart_items['id'], $cart_items['qty']);
		    	}
		    	$url_success = 'http://dota2shop.vn/shop/payment/BaoKimConfirm/success';
		    	$url_cancel =  'http://dota2shop.vn/shop/payment/BaoKimConfirm/'.$invoice_id;
		    	$url_detail = "http://dota2shop.vn/shop/payment/load_invoice_detail_by_invoiceid/".$invoice_id;
		    	$this->load->library('baokimpayment');
		    	$url = $this->baokimpayment->createRequestUrl($invoice_id, 'hiroshipham@gmail.com', $this->cart->total(), 0, 0, '', $url_success, $url_cancel, $url_detail);
		    	
		    	$this->cart->destroy();
		    	redirect($url);
	    	}

    	}
        else
        {
            if($type == 'ATM')
            {
                $bankthanhtoan = $this->input->post('nganhangthanhtoan_1');
            }
            else if($type == 'IB')
            {
                $bankthanhtoan = $this->input->post('nganhangthanhtoan_2');
            }
            else if($type == 'CK')
            {
                $bankthanhtoan = $this->input->post('nganhangthanhtoan');
            }
            
            $description = "Thanh toán qua $type qua ngân hàng: $bankthanhtoan";
            if($this->m_payment->insert_invoice($accountid, $type, $start_date, $total, 1, $description))
            {
                $invoice_id = $this->db->insert_id();
                $data['type'] = $type;
                $data['invoice_id'] = $invoice_id;
                $data['total'] = $total;
                foreach($this->cart->contents() as $cart_items)
                {

                    $this->m_payment->insert_invoice_detail($invoice_id, $cart_items['id'], $cart_items['qty']);

                }
                $this->cart->destroy();
                $data['invoice_details'] = $this->m_payment->load_invoice_detail_by_invoiceid($invoice_id);
                $data['isdone'] = true;
                $this->load->view('Payment/Complete_payment', $data);
            }
        }
    }

    function BaoKimConfirm($result)
    {
    	if($result == 'success')
    	{
    		$data['isdone'] = true;
    		$data['type'] = 'BK';
    		$this->load->view('Payment/Complete_payment', $data);
    	}
    	else
    	{
    		$this->load->model('m_invoice');
    		date_default_timezone_set('Asia/Ho_Chi_Minh');
    		$now = getdate();

    		$END_DATE = $now['year']."-".$now['mon']."-".$now['mday']." ".$now['hours'].":".$now['minutes'].":".$now['seconds'];
    		$PAYMENT_DESCRIPTION = 'Hủy đơn hàng từ Bảo Kim';

    		$this->m_invoice->update_don_hang_status($result,$END_DATE,0,$PAYMENT_DESCRIPTION);
    		redirect();
    	}
    }


}

?>